<?php
class EventFormHelper extends AppHelper
{
		var $helpers = array("Html","Javascript");
        
		function showInputField($form,$field,$options = array())
		{
			$out = "";
			if($field == "event_cost_to")
			{
				return $out;
			}
			if($field == "event_cost")
			{
				$out .= "<div class='event_cost_field_wrapper'>";
				$out .= $form->input("event_cost");
				$out .= $form->input("event_cost_to");
				$out .= "</div>";
			} else if($field == "event_from" || $field == "event_to") {
				$out .= "<div class='event_dates_field_wrapper'>";
				$label = $this->getEventModelObject()->getFieldProperty($field,"label");
				$out .= $form->input("Event.".$field."_dummy.date",array("type"=>"datePicker","label"=>$label));//,"style"=>"width:200px;","div"=>"false"
				$hoursOptions = array();
				for($i =0 ;$i <=23;$i++)
				{
					$meridiem = "am";
					$hour = $i;
					if($i >= 12)
					{
						if($i > 12)
						{
							$hour = $i -12;
						}
						$meridiem = "pm";
					}
					if($hour == 0 && $meridiem == "am")
					{
						$hour = 12;
					}
					$hoursOptions[sprintf("%2d:00:00",$i)] = sprintf("%02d:00 %s",$hour,$meridiem);
					$hoursOptions[sprintf("%2d:30:00",$i)] = sprintf("%02d:30 %s",$hour,$meridiem);
				}
				$out .= "&nbsp;&nbsp;&nbsp;";
				$out .= $form->input("Event.".$field."_dummy.time",array("label"=>"&nbsp;","options"=>$hoursOptions,"type"=>"select","empty"=>false));//,"div"=>false,"style"=>"float:left;"
				$out .= $form->input($field,array("div"=>false,"label"=>false,"type"=>"text","style"=>"display:none"));
				$out .= "</div>";
				
				$fieldName = "#".Inflector::camelize("event_".$field);
				$dateFieldName = $fieldName."DummyDate";
				$timeFieldName = $fieldName."DummyTime";
				$js =<<< END
	        	\$("{$dateFieldName}").datepicker("option", "minDate", "1");
	        	if(\$("{$fieldName}").val())
	            {
	            	\$("{$dateFieldName}").val(\$("{$fieldName}").val().split(" ")[0]);
	                \$("{$timeFieldName}").val(\$("{$fieldName}").val().split(" ")[1]).trigger("change");
	            }
	            \$("{$dateFieldName},{$timeFieldName}").on("change",function(){
	            	var dateValue = \$("{$dateFieldName}").val();
	                var timeValue = \$("{$timeFieldName}").val();
	                if(dateValue && timeValue)
	                {
	                    \$("{$fieldName}").val(dateValue+" "+timeValue);
	                }
	            });
END;
				$this->Javascript->startup($js);
			} else {
				$out = $form->input($field);
			}
			return $out;
		}
	
		function getEventModelObject()
		{
			return cri("Events.Event");
		}
        
}
?>